package com.sgb.kjwl.view.widget;


import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;


import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.QueryTwoCarrierVO;
import com.sgb.kjwl.utils.CargoConstant;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.view.ui.activity.transport.CarriageDetailActivity;
import com.swgk.core.util.DateUtils;

public class ItemDeliverView extends LinearLayout {

    private View rootView;
    private QueryTwoCarrierVO queryTwoCarrierVO;
    private Context context;
    private boolean isDeliverListPage;

    public ItemDeliverView(Context context) {
        this(context, null, false);
    }

    public ItemDeliverView(Context context, QueryTwoCarrierVO queryTwoCarrierVO) {
        this(context, queryTwoCarrierVO, false);
    }

    public ItemDeliverView(Context context, boolean isDeliverListPage) {
        this(context, null, isDeliverListPage);
    }

    public ItemDeliverView(Context context, QueryTwoCarrierVO queryTwoCarrierVO, boolean isDeliverListPage) {
        this(context, null, queryTwoCarrierVO, isDeliverListPage);
    }

    public ItemDeliverView(Context context, @Nullable AttributeSet attrs, QueryTwoCarrierVO queryTwoCarrierVO, boolean isDeliverListPage) {
        super(context, attrs);
        this.queryTwoCarrierVO = queryTwoCarrierVO;
        this.context = context;
        this.isDeliverListPage = isDeliverListPage;
        initView();
    }

    private void initView() {
        rootView = inflate(context, R.layout.item_deliver_info_layout, this);
        setOrientation(VERTICAL);
        int dp16 = getResources().getDimensionPixelOffset(R.dimen.dp16);
        int dp15 = getResources().getDimensionPixelOffset(R.dimen.dp15);
        int dp10 = getResources().getDimensionPixelOffset(R.dimen.dp10);
        if (isDeliverListPage) {
            rootView.findViewById(R.id.split_line).setVisibility(VISIBLE);
            setBackgroundColor(Color.parseColor("#ffffff"));
            setPadding(dp15, dp15, dp15, dp15);
        } else {
            setPadding(dp10, dp16, dp16, dp16);
            rootView.findViewById(R.id.split_line).setVisibility(GONE);
            setBackground(getResources().getDrawable(R.drawable.item_deliver_info_bkg));
        }

        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.topMargin = dp10;
        setLayoutParams(params);
        bindData();
    }

    public void bindData(QueryTwoCarrierVO queryTwoCarrierVO) {
        this.queryTwoCarrierVO = queryTwoCarrierVO;
        bindData();
    }

    public void bindData() {
        if (queryTwoCarrierVO == null) {
            return;
        }
        ((TextView) rootView.findViewById(R.id.deliver_path_province_town)).setText(queryTwoCarrierVO.getProvinceName() + " " + queryTwoCarrierVO.getCityName());
        ((TextView) rootView.findViewById(R.id.deliver_path_area)).setText(queryTwoCarrierVO.getDistrictName());
        ((TextView) rootView.findViewById(R.id.receive_path_province_town)).setText(queryTwoCarrierVO.getDestProvinceName() + " " + queryTwoCarrierVO.getDestCityName());
        ((TextView) rootView.findViewById(R.id.receive_path_area)).setText(queryTwoCarrierVO.getDestDistrictName());

        if (queryTwoCarrierVO.getShipmentEndTime() == 0 ||
                queryTwoCarrierVO.getShipmentStartTime() == queryTwoCarrierVO.getShipmentEndTime()) {
            ((TextView) rootView.findViewById(R.id.load_goods_time)).setText(DateUtils.stampToDay5(queryTwoCarrierVO.getShipmentStartTime()));
        } else {
            ((TextView) rootView.findViewById(R.id.load_goods_time)).setText(DateUtils.stampToDay5(queryTwoCarrierVO.getShipmentStartTime()) + " - " + DateUtils.stampToDay5(queryTwoCarrierVO.getShipmentEndTime()));
        }

        if (queryTwoCarrierVO.getDiscuss() == 1) {
            ((TextView) rootView.findViewById(R.id.freight)).setText("商议");
        } else {
            ((TextView) rootView.findViewById(R.id.freight)).setText(NumberUtil.trim0(queryTwoCarrierVO.getKilometre()) + "公里内"
                    + NumberUtil.trim0(queryTwoCarrierVO.getFreight())
                    + queryTwoCarrierVO.getFreightUnit());
        }
        rootView.setOnClickListener(v -> {
            CarriageDetailActivity.start(getContext(), queryTwoCarrierVO.getCarrierNo(), 1);
        });
    }


}
